package com.example.langchanin4jdemo1.controller;

import com.example.langchanin4jdemo1.config.MyDocumentSplitter;
import dev.langchain4j.community.model.dashscope.QwenEmbeddingModel;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentParser;
import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.TextDocumentParser;
import dev.langchain4j.data.document.splitter.DocumentByLineSplitter;
import dev.langchain4j.data.document.splitter.DocumentByParagraphSplitter;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.redis.RedisEmbeddingStore;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class RagSearchDemo {
    public static void main(String[] args) throws Exception{
        Path documentPath =
                Paths.get(RagSearchDemo.class.getClassLoader().getResource("question.txt").toURI());
        DocumentParser documentParser = new TextDocumentParser();
        Document document = FileSystemDocumentLoader.loadDocument(documentPath,
                documentParser);

        //文本切分
//        DocumentSplitter splitter = new MyDocumentSplitter();
        DocumentByParagraphSplitter splitter =new DocumentByParagraphSplitter(1000,100);
        List<TextSegment> segments = splitter.split(document);

        //文本向量化村粗
        EmbeddingModel embeddingModel = QwenEmbeddingModel
                .builder()
                .apiKey("sk-875dd6ef14244431acdc7ccb974f5bfe")
                .modelName("text-embedding-v2")
                .build();

        RedisEmbeddingStore embeddingStore = RedisEmbeddingStore.builder()
                .host("127.0.0.1")
                .port(6379)
                .dimension(1536) //维度，需要与计算结果保持⼀致。如果使⽤其他的模型，可能会有不同的结果。
                .indexName("service_rag")
                .build();

        List<Embedding> embeddings = embeddingModel.embedAll(segments).content();
        embeddingStore.addAll(embeddings,segments);
        System.out.println("文档加载向量数据库成功");
    }
}
